import request from '@/utils/request'

export const workflowApi = {
  // 获取工作流列表
  getWorkflows(params = {}) {
    return request.get('/workflows', { params })
  },

  // 获取工作流详情
  getWorkflowById(id) {
    return request.get(`/workflows/${id}`)
  },

  // 创建工作流
  createWorkflow(workflow) {
    return request.post('/workflows', workflow)
  },

  // 更新工作流
  updateWorkflow(id, workflow) {
    return request.put(`/workflows/${id}`, workflow)
  },

  // 删除工作流
  deleteWorkflow(id) {
    return request.delete(`/workflows/${id}`)
  },

  // 启用/禁用工作流
  toggleWorkflowStatus(id, enabled) {
    return request.put(`/workflows/${id}/status`, { enabled })
  },

  // 启动工作流实例
  startWorkflowInstance(workflowId, inputData = {}) {
    return request.post(`/workflows/${workflowId}/instances`, { inputData })
  },

  // 获取工作流实例列表
  getWorkflowInstances(workflowId, params = {}) {
    return request.get(`/workflows/${workflowId}/instances`, { params })
  },

  // 获取工作流实例详情
  getWorkflowInstance(workflowId, instanceId) {
    return request.get(`/workflows/${workflowId}/instances/${instanceId}`)
  },

  // 终止工作流实例
  terminateInstance(workflowId, instanceId, reason = '') {
    return request.post(`/workflows/${workflowId}/instances/${instanceId}/terminate`, { reason })
  },

  // 重启工作流实例
  restartInstance(workflowId, instanceId) {
    return request.post(`/workflows/${workflowId}/instances/${instanceId}/restart`)
  },

  // 获取任务列表
  getTasks(params = {}) {
    return request.get('/tasks', { params })
  },

  // 获取待办任务
  getPendingTasks(userId, params = {}) {
    return request.get('/tasks/pending', {
      params: { userId, ...params }
    })
  },

  // 完成任务
  completeTask(taskId, data = {}) {
    return request.post(`/tasks/${taskId}/complete`, data)
  },

  // 拒绝任务
  rejectTask(taskId, reason = '') {
    return request.post(`/tasks/${taskId}/reject`, { reason })
  },

  // 委派任务
  delegateTask(taskId, assigneeId) {
    return request.post(`/tasks/${taskId}/delegate`, { assigneeId })
  },

  // 获取任务表单
  getTaskForm(taskId) {
    return request.get(`/tasks/${taskId}/form`)
  },

  // 提交任务表单
  submitTaskForm(taskId, formData) {
    return request.post(`/tasks/${taskId}/form`, formData)
  },

  // 获取工作流模板
  getWorkflowTemplates() {
    return request.get('/workflow-templates')
  },

  // 根据模板创建工作流
  createWorkflowFromTemplate(templateId, workflowData) {
    return request.post(`/workflow-templates/${templateId}/create-workflow`, workflowData)
  },

  // 获取工作流统计
  getWorkflowStats(workflowId, timeRange = '30d') {
    return request.get(`/workflows/${workflowId}/stats`, {
      params: { timeRange }
    })
  },

  // 获取工作流执行历史
  getWorkflowHistory(workflowId, params = {}) {
    return request.get(`/workflows/${workflowId}/history`, { params })
  },

  // 导出工作流定义
  exportWorkflow(workflowId) {
    return request.get(`/workflows/${workflowId}/export`, {
      responseType: 'blob'
    })
  },

  // 导入工作流定义
  importWorkflow(workflowFile) {
    const formData = new FormData()
    formData.append('workflow', workflowFile)
    return request.post('/workflows/import', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  },

  // 验证工作流定义
  validateWorkflow(workflowData) {
    return request.post('/workflows/validate', workflowData)
  },

  // 获取工作流节点类型
  getNodeTypes() {
    return request.get('/workflow-node-types')
  },

  // 获取工作流连接器
  getConnectors() {
    return request.get('/workflow-connectors')
  },

  // 测试工作流连接器
  testConnector(connectorId, config) {
    return request.post(`/workflow-connectors/${connectorId}/test`, config)
  }
}
